package com.project.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan("com.project")
@MapperScan("com.project.mapper")
@EnableTransactionManagement
public class MyBatisConfig {
    @Bean
    public DruidDataSource getData(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/maven?characterEncoding=utf8&allowMultiQueries=true");
        dataSource.setUsername("root");
        dataSource.setPassword("083651");
        dataSource.setMaxActive(100);
        dataSource.setMinIdle(20);
        dataSource.setMaxWait(1000);
        return dataSource;
    }
    @Bean
    public SqlSessionFactoryBean factoryBean(){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(this.getData());
        factoryBean.setConfigLocation(new ClassPathResource("mybatis.cfg.xml"));
        return factoryBean;
    }
    @Bean
    public TransactionManager getTransactionManager(){
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(getData());
        return manager;
    }
}
